package com.rykj.qxj.test.xtest;

import java.util.ArrayList;
import java.util.List;

public class Offer {
	public Node search(Node head, int k) {
		if (head == null)
			return null;
		int count = 1;
		Node old = head;
		
		while (head.getNext() != null) {
			head = head.getNext();
			count++;
		}
		for (int i = 0; i < count - k; i++) {
			old = old.getNext();
		}
		return old;
	}

	public static void main(String[] args) {
		Node first = new Node();
		Node second = new Node();
		Node third = new Node();
		Node fourth = new Node();
		first.setData(1);
		first.setNext(second);
		
		second.setData(2);
		second.setNext(third);
		
		third.setData(3);
		third.setNext(fourth);
		
		fourth.setData(4);
		fourth.setNext(null);
		//继续添加节点.....
		
		Offer offer1 = new Offer();
		Node result = offer1.search(first, 1);
		List<Node> nodes =new ArrayList<Node>();
		nodes.add(first);
		nodes.add(second);
		nodes.add(third);
		nodes.add(fourth);
		System.out.print("链表数据为:");
		for (Node node : nodes) {
			System.out.print(node.getData()+" ");
		}
		System.out.println();
		System.out.println("输入节点的数据是:"+first.getData());
		System.out.println("这个节点在链表中倒数位置是:"+result.getData());
	}

}
